package com.klangner.behaviors
{
    import com.klangner.geom.Vector2D;
    import com.klangner.move.behavior.IBehavior;
    import com.klangner.move.behavior.ISteeredObject;

    public class SteeredVehicle extends Vehicle implements ISteeredObject
    {
        private var _maxForce:Number = 1;
        private var _desiredVelocity:Vector2D = new Vector2D();
        private var _behaviors:Array = new Array();
        

        public function SteeredVehicle(){
        }
        
        public function addBehavior(behavior:IBehavior):void{
          _behaviors.push(behavior);
        }

        public function setDesiredVelocity(desiredVelocity:Vector2D):void{
          if(null == _desiredVelocity)
            _desiredVelocity = desiredVelocity;
          else
            _desiredVelocity.add(desiredVelocity);
        }
        
        public function set maxForce(value:Number):void
        {
            _maxForce = value;
        }
        public function get maxForce():Number
        {
            return _maxForce;
        }

        override public function update():void
        {
          _desiredVelocity = null;
//          _desiredVelocity = _velocity.clone();
          for each(var behavior:IBehavior in _behaviors){
            behavior.updateForce(this);
          }
          
          if(_desiredVelocity.length > 1){
            _desiredVelocity.normalize();
          }

          _desiredVelocity.multiply(_maxSpeed);
          _desiredVelocity.divide(_mass);
          _velocity = _desiredVelocity;
          super.update();
        }
     }
}

